home *** CD-ROM | disk | FTP | other *** search
- ;; isearch.mut: Incremental searching.
-
- ;; Searching as you type - shows you where the string you have typed so
- ;; far would be found.
-
- ;; Keys:
- ;; ^H (Backspace): Remove the last character from the search pattern.
- ;; Don't move.
- ;; ^S: Search forward for existing pattern. If haven't entered a
- ;; pattern, the old pattern is used.
- ;; ^R: Search reverse for pattern.
- ;; ^Q: Quote the next character into the search pattern. Useful for
- ;; searching for control characters.
- ;; ^W: Add the rest of the word to the search pattern.
- ;; ^G: Exit search.
- ;; ^M (Enter): Drop to non incremental search if haven't done anything
- ;; yet.
- ;; Control characters and combo keys (eg cursor motion, C-X=, etc)
- ;; terminate the search. The key is then executed. For example,
- ;; searching for text and then pressing C-P will cause the cursor to
- ;; move to the line above the matched text.
-
- ;; If you want to call isearch from a pgm, prime and start searching,
- ;; just pass in a search string. eg (isearch TRUE "foobar") will start a
- ;; incremental search at the point and search for foobar.
-
- ;; C Durland Public Domain
-
- (defun
- isearch-forward { (isearch TRUE) } ;; Normally bound to C-S
- isearch-reverse { (isearch FALSE) } ;; Normally bound to C-R
- MAIN
- {
- (bind-to-key "isearch-forward" "C-s")
- (bind-to-key "isearch-forward" 'C-\')
- (bind-to-key "isearch-reverse" "C-r")
- }
- )
-
-
- (include me2.h)
- (string oldpat)
-
- (defun isearch (bool forward-search) ;; [(string pattern)]
- {
- (string ipat hoho tmp direction)
- (int sc len)
- (bool firsttime forward)
-
- (direction (if (forward forward-search) "forward" "reverse"))
- (ipat (hoho ""))
-
- ;; check to see pattern was passed in
- (if (== 2 (nargs)) { (ipat (arg 1))(len 0)(goto ack) })
-
- (firsttime TRUE)
- (while TRUE
- {
- (msg "I-search-" direction " [" ipat "] " hoho ' (^S ^R ^W ^Q)')
- (if (not (key-waiting)) (update))
- (hoho "")
- (switch (sc (get-key))
- 0x148 (ipat (extract-elements ipat 0 -1)) ;; backspace
- 0x153 ;; ^S: forward search, same pattern
- {
- (label I-hate-XON/XOFF)
- (forward TRUE)(direction "forward")
- (if firsttime
- {
- (firsttime FALSE)
- (if (== oldpat "")(continue))
- (ipat oldpat)
- })
- (if (not (search-forward ipat))(hoho "Not found."))
- }
- 0x15C (goto I-hate-XON/XOFF) ;; ^\: same as ^S
- 0x152 ;; ^R: reverse the search
- {
- (forward FALSE)(direction "reverse")
- (if firsttime
- {
- (firsttime FALSE)
- (if (== oldpat "")(continue))
- (ipat oldpat)
- (if (search-reverse ipat)
- (search-forward ipat) ;; put dot at end of pattern
- (hoho "Not found.")
- )
- }
- {
- (arg-prefix (length-of ipat))(previous-character)
- (if (search-reverse ipat)
- (search-forward ipat) ;; put dot at end of pattern
- {
- (arg-prefix (length-of ipat))(next-character)
- (hoho "Not found.")
- }
- )
- }
- )
- }
- 0x151 ;; ^Q: quote next character
- {
- (len (length-of ipat))
- (ipat (concat ipat (getchar)))
- (goto ack)
- }
- 0x157 ;; ^W: grab word
- {
- (if (looking-at '\w+')
- {
- (len (length-of ipat))
- (ipat (concat ipat (get-matched '&')))
- (goto ack) ; we know the search will succeed
- })
- (hoho "Not looking at a word.")
- }
- 0x14D ;; ^M: drop down into non incremental search
- {
- (if firsttime
- {
- ;;;;!!! use a default (if available)
- (ask-user)(ipat (ask "Search for: "))
- (if forward (search-forward ipat) (search-reverse ipat))
- (goto done-searching)
- })
- }
- 0x147 ;; ^G: quit
- {
- (msg "Done.")
- (label done-searching)
- (if (!= "" ipat) (oldpat ipat)) ; save search pattern (if interesting).
- (done)
- }
- default
- {
- (if (> sc 0xFF) { (msg "")(exe-key sc)(goto done-searching) })
- (len (length-of ipat))
- ; (ipat (concat ipat (chr$ sc tmp)))
- (ipat (concat ipat (convert-to CHARACTER sc)))
- (label ack)
- (firsttime FALSE)
- (if forward
- {
- (arg-prefix len)(previous-character) ;; move to start of pattern
- (if (not (search-forward ipat))
- {
- (arg-prefix len)(next-character)
- (label gack)
- (ipat (extract-elements ipat 0 -1))(hoho "Not found.")
- })
- }
- {
- (if (search-reverse ipat)
- (search-forward ipat)
- (goto gack)
- )
- })
- })
- })
- })
-